from itertools import chain

import jieba.posseg as pos
import matplotlib.pyplot as plt
import pandas as pd
from wordcloud import WordCloud


def show_word(file_path):
    train_data = pd.read_csv(file_path, sep='\t')
    train_data_positive = train_data[train_data['label'] == 1]['sentence']
    set1 = list(chain(*map(lambda x: pos.lcut(x), train_data_positive)))
    # set1 = list(chain(pos.lcut(x) for x in train_data['sentence']))[0]
    # set1 = list(chain.from_iterable(pos.lcut(x) for x in train_data_positive))
    # print(set1[:10])
    # set1是个list,每个元素是Pair, Pair有2字段:word,flag(词性,a为形容词)
    # filtered_list = ' '.join(list(map(lambda x: x.word, filter(lambda x: x.flag == 'a', set1))))
    space_separated_word = ' '.join(x.word for x in set1 if x.flag == 'a')
    print(space_separated_word)

    font_path = "C:/Windows/Fonts/simhei.ttf"  # 黑体字体路径
    wordcloud = WordCloud(max_words=100, font_path=font_path).generate(space_separated_word)
    plt.figure(figsize=(10, 8))
    plt.imshow(wordcloud, interpolation='bilinear')
    plt.axis('off')
    plt.show()


if __name__ == '__main__':
    show_word('./train.tsv')
    show_word('./dev.tsv')
